package com.example.service.impl;

import com.example.domain.MeetingApplication;
import com.example.domain.MeetingSheet;
import com.example.mapper.MeetingApplicationMapper;
import com.example.mapper.MeetingSheetMapper;
import com.example.service.IMeetingApplicationService;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 会议室申请管理Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class MeetingApplicationServiceImpl implements IMeetingApplicationService
{
    @Autowired
    private MeetingApplicationMapper meetingApplicationMapper;
    @Autowired
    private MeetingSheetMapper meetingSheetMapper;


    /**
     * 查询会议室申请管理
     * 
     * @param id 会议室申请管理主键
     * @return 会议室申请管理
     */
    @Override
    public MeetingApplication selectMeetingApplicationById(Long id)
    {
        return meetingApplicationMapper.selectMeetingApplicationById(id);
    }

    /**
     * 查询会议室申请管理列表
     * 
     * @param meetingApplication 会议室申请管理
     * @return 会议室申请管理
     */
    @Override
    public List<MeetingApplication> selectMeetingApplicationList(MeetingApplication meetingApplication)
    {
        return meetingApplicationMapper.selectMeetingApplicationList(meetingApplication);
    }

    /**
     * 新增会议室申请管理
     * 
     * @param meetingApplication 会议室申请管理
     * @return 结果
     */
    @Override
    public int insertMeetingApplication(MeetingApplication meetingApplication)
    {
        TestTime(meetingApplication.getPlannedStartTime(),meetingApplication.getPlannedEndTime());
        TestNember(meetingApplication.getPlannedStartTime(),meetingApplication.getPlannedEndTime(),meetingApplication.getMeetingRoomId());
        meetingApplication.setStatus( 0L );
        Long userId = SecurityContextHolder.getUserId();
        SysUser user = meetingApplicationMapper.selectUserById( userId );
        Long deptId = user.getDeptId();
        String deptname =meetingApplicationMapper.selectDeptName(deptId);
        meetingApplication.setDeptId( deptId );
        meetingApplication.setDeptName( deptname );
        meetingApplication.setApplicant( user.getNickName() );
        return meetingApplicationMapper.insertMeetingApplication(meetingApplication);
    }

    private void TestNember(Date StartTime, Date EndTime, Long meetingRoomId) {
        int number = meetingApplicationMapper.selectByRoomIdandTime(StartTime,EndTime,meetingRoomId);
        if (number>0){
            throw new RuntimeException("此间会议室在次时间段有人使用");
        }
    }

    /**
     * 时间验证
     * @param StartTime
     * @param EndTime
     */
    private void TestTime(Date StartTime, Date EndTime) {
        if (EndTime.compareTo( StartTime )<=0){
            throw  new RuntimeException("时间不合法！");
        }
    }

    /**
     * 修改会议室申请管理
     * 
     * @param meetingApplication 会议室申请管理
     * @return 结果
     */
    @Override
    public int updateMeetingApplication(MeetingApplication meetingApplication)
    {
        int i = meetingApplicationMapper.updateMeetingApplication( meetingApplication );
        MeetingApplication meet= new MeetingApplication();
        meet.setStatus( 1L );
        List<MeetingApplication> list = meetingApplicationMapper.selectMeetingApplicationList( meet );
        for (MeetingApplication application : list) {
            MeetingSheet meetingSheet = new MeetingSheet();
            meetingSheet.setMeetingTitle( application.getMeetingTitle() );
            meetingSheet.setPlannedStartTime( application.getPlannedStartTime() );
            meetingSheet.setPlannedEndTime( application.getPlannedEndTime() );
            int number =  meetingSheetMapper.selectNumberBy(application.getMeetingTitle() );
            meetingSheet.setNumber( (long) number );
            meetingSheet.setStatus( 0L );
            meetingSheetMapper.insertMeetingSheet( meetingSheet );
        }
        return i;
    }

    /**
     * 批量删除会议室申请管理
     * 
     * @param ids 需要删除的会议室申请管理主键
     * @return 结果
     */
    @Override
    public int deleteMeetingApplicationByIds(Long[] ids)
    {
        return meetingApplicationMapper.deleteMeetingApplicationByIds(ids);
    }

    /**
     * 删除会议室申请管理信息
     * 
     * @param id 会议室申请管理主键
     * @return 结果
     */
    @Override
    public int deleteMeetingApplicationById(Long id)
    {
        return meetingApplicationMapper.deleteMeetingApplicationById(id);
    }
}
